library(readr)
library(plotly)
## Warning: package 'plotly' was built under R version 4.1.3
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)
library(DMwR2)
## Warning: package 'DMwR2' was built under R version 4.1.3
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## Warning: package 'lubridate' was built under R version 4.1.3
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(hrbrthemes)
## Warning: package 'hrbrthemes' was built under R version 4.1.3
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
#setwd("E:/QMSS/DV/Group_V_Global_Temperature/GlobalLandTemperaturesByCountry")
getwd()
## [1] "E:/QMSS/DV/Group_V_Global_Temperature"
Country<-read_csv("E:/QMSS/DV/Group_V_Global_Temperature/GlobalLandTemperaturesByCountry.csv")
## Rows: 577462 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (1): Country
## dbl  (2): AverageTemperature, AverageTemperatureUncertainty
## date (1): dt
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
class(Country$dt)
## [1] "Date"
New_Country<-Country%>%
  filter(AverageTemperature!="N/A"& AverageTemperatureUncertainty!="N/A")
New_Country<-New_Country%>%
  filter(dt >="1970-01-01")
New_Country<-New_Country %>%
    group_by(Country) %>%
  
    mutate(Diff_Temp = AverageTemperature - lag(AverageTemperature,12))
USA<-New_Country%>%
  filter(Country %in% c("United States"))
class(USA$dt)
## [1] "Date"
Japan<-New_Country%>%
  filter(Country %in% c("Japan"))
Russia<-New_Country%>%
  filter(Country %in% c("Russia"))
plot1<- ggplot() +
    geom_line(data = Russia, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Russia, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
    ggtitle("Russia Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1990-01-01"),as.Date("2013-09-11")))+
   theme_minimal()

  
ggplotly(plot1)
#plot1
plot2<-ggplot()+
    geom_line(data = USA, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = USA, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
  ggtitle("USA Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1990-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot2)
plot3<-ggplot()+
    geom_line(data = Japan, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Japan, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   ggtitle("Japan Monthly Temperature and Difference with previous year from 1990 to 2013")+
   labs(color="Tempt_Type")+
    scale_x_date(limit=c(as.Date("1990-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot3)
avg_diff<-New_Country%>%
  group_by(Country)%>%
  summarise(Variance = var(AverageTemperature))%>%
  arrange(Variance)  %>%
  mutate(rank = row_number())%>%
  filter(rank<=10)
class(New_Country$Diff_Temp)
## [1] "numeric"
avg_diff_by_tempdiff<-New_Country%>%
  filter(Diff_Temp!="N/A")%>%
  group_by(Country)%>%
  summarise(meanD = mean(Diff_Temp))
Singapore<-New_Country%>%
  filter(Country %in% c("Singapore"))
plot4<-ggplot()+
    geom_line(data = Singapore, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Singapore, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("Singapore Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot4)
Indonesia<-New_Country%>%
  filter(Country %in% c("Colombia"))
plot4<-ggplot()+
    geom_line(data = Indonesia, aes(x=dt, y=AverageTemperature,color="AverageTemperature")) +
    geom_line(data = Indonesia, aes(x=dt, y=Diff_Temp,color="Diff_Temp"))+
    xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("Columbia Monthly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(plot4)
By_Year<-New_Country%>%
  mutate(year = substring(dt,0,4))%>%
  group_by(Country,year)%>%
  summarise(Year_Tempt = mean(AverageTemperature))%>%
  mutate(Year_diff = Year_Tempt-lag(Year_Tempt))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
By_Year$year<-ymd(By_Year$year, truncated = 2L)
class(By_Year$year)
## [1] "Date"
By_Year_USA<-By_Year%>%
  filter(Country %in% c("United States"))
Year_1<-ggplot()+
    geom_line(data = By_Year_USA, aes(x=year, y=Year_Tempt,color="Year_Tempt")) +
    geom_line(data = By_Year_USA, aes(x=year, y=Year_diff,color="Year_diff"))+
      xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("USA Yearly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(Year_1)
By_Year_Russia<-By_Year%>%
  filter(Country %in% c("Russia"))
Year_2<-ggplot()+
    geom_line(data = By_Year_Russia, aes(x=year, y=Year_Tempt,color="Year_Tempt")) +
    geom_line(data = By_Year_Russia, aes(x=year, y=Year_diff,color="Year_diff"))+
      xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("Russia Yearly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(Year_2)
By_Year_Japan<-By_Year%>%
  filter(Country %in% c("Japan"))
Year_3<-ggplot()+
    geom_line(data = By_Year_Japan, aes(x=year, y=Year_Tempt,color="Year_Tempt")) +
    geom_line(data = By_Year_Japan, aes(x=year, y=Year_diff,color="Year_diff"))+
      xlab("Dates")+
    ylab("Tempt")+
   labs(color="Tempt_Type")+
   ggtitle("JP Yearly Temperature and Difference with previous year from 1990 to 2013")+
    scale_x_date(limit=c(as.Date("1971-01-01"),as.Date("2013-09-11")))+
   theme_minimal()
ggplotly(Year_3)
Continents<-New_Country%>%
  filter(Country %in% c("Asia", "Africa", "North America", "South America", "Antarctica", 'Europe','Oceania'))
Vio_1<-ggplot(data = Continents, aes(x= Country,y=AverageTemperature,fill=Country))+
  geom_violin() +
  geom_boxplot(width=0.1, color="grey", alpha=0.2)+
   theme_minimal()
  
ggplotly(Vio_1)